﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyNhaSach.Customer
{
    public partial class FrmCustomerList : Form
    {

        #region Private Variable

        BindingList<CustomerDTO> listCustomerBinding;
        int rowIndex; //Biến lưu giá trị của dòng đang sửa hơặc vừa thêm mới
        CustomerBUS customerBUS;
        RuleObjectBUS RuleBUS;

        #endregion Private Variable

        #region Constructor
        public FrmCustomerList()
        {
            InitializeComponent();
            customerBUS = new CustomerBUS();
            RuleBUS = new RuleObjectBUS();
            dtgCustomerList.AutoGenerateColumns = false;
            rowIndex = 0;
            listCustomerBinding = new BindingList<CustomerDTO>();
            colCustomerBirthday.DefaultCellStyle.Format = Global.DateTimeFormat;
            colCustomerBeginday.DefaultCellStyle.Format = Global.DateTimeFormat;
        }
        #endregion Constructor

        private void dtgCustomerList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (e.ColumnIndex == colEdit.Index)
                {
                    UpdateCustomer(e.RowIndex);
                }
                else if (e.ColumnIndex == colDel.Index)
                {
                    if (!RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject. ActionCustomer))
                    {
                        Global.SetMessage(lblMessage, "Bạn không có quyền thực hiện thao tác này!", false);
                        return;
                    }

                    if (Global.ShowMessageBoxDelete("Bạn chắc chắn muốn xóa khách hàng này ?") == DialogResult.Yes)
                    {
                        int cusID = int.Parse(dtgCustomerList[colCustomerID.Index, dtgCustomerList.CurrentRow.Index].Value.ToString());
                        DeleteCustomer(cusID);
                    }
                }
            }
        }

        private void FrmCustomerList_Load(object sender, EventArgs e)
        {
            listCustomerBinding = customerBUS.GetNewBindingListCustomer();
            dtgCustomerList.DataSource = listCustomerBinding;
            this.VisibleChanged += new EventHandler(FrmCustomerList_VisibleChanged);
            CheckPermission();
        }

        private void CheckPermission()
        {
            btnDel.Enabled = RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionAuthor);
            btnUpdate.Enabled = btnDel.Enabled;
        }

        void FrmCustomerList_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                Global.GenerateNumber(dtgCustomerList, colNumber.Index);
                Global.GenerateEditColumn(dtgCustomerList, colEdit.Index);
                Global.GenerateDeleteColumn(dtgCustomerList, colDel.Index);
            }
        }

        private void UpdateCustomer(int index)
        {
            if (!RuleBUS.CheckPermission(Global.GroupID, (int)RuleObject.ActionCustomer))
            {
                Global.SetMessage(lblMessage, "Bạn không có quyền thực hiện thao tác này!", false);
                return;
            }

            rowIndex = index;
            bool sex = false;
            if(dtgCustomerList[colCustomerSex.Index,rowIndex].Value.ToString() == "Nam")
                sex = true;
            CustomerDTO customerDTO = new CustomerDTO
            {
                CustomerID = int.Parse(dtgCustomerList[colCustomerID.Index, rowIndex].Value.ToString()),
                CustomerCode = dtgCustomerList[colCustomerCode.Index, rowIndex].Value.ToString(),
                CustomerName = dtgCustomerList[colCustomerName.Index, rowIndex].Value.ToString(),
                CustomerAddress = dtgCustomerList[colCustomerAddress.Index,rowIndex].Value.ToString(),
                CustomerBeginDay = (DateTime) dtgCustomerList[colCustomerBeginday.Index,rowIndex].Value,
                CustomerBirthday = (DateTime) dtgCustomerList[colCustomerBirthday.Index,rowIndex].Value,
                CustomerCMND = dtgCustomerList[colCustomerCMND.Index, rowIndex].Value.ToString(),
                CustomerDebtMoney = (float)dtgCustomerList[colCustomerDebtMoney.Index,rowIndex].Value,
                CustomerEmail = dtgCustomerList[colCustomerEmail.Index, rowIndex].Value.ToString(),
                CustomerPhone = dtgCustomerList[colCustomerPhone.Index, rowIndex].Value.ToString(),
                CustomerSex = sex,
                Note = dtgCustomerList[colCustomerNote.Index,rowIndex].Value.ToString()
            };


            Customer.FrmCustomerDetail frm = new FrmCustomerDetail { CustomerDto = customerDTO, Action = ActionName.Update };
            //frm.OnCategoryChanged += new Book.FrmCategoryDetail.CategoryHasChanged(CategoryChanged);
            frm.ShowDialog();
        }

        private void DeleteCustomer(int cusID)
        {
            if (dtgCustomerList.Rows.Count > 0)
            {
                
                    if (customerBUS.DeleteCustomer(cusID))
                    {
                        Global.SetMessage(lblMessage, "Xóa thành công", true);
                        listCustomerBinding.Remove(listCustomerBinding.First(c => c.CustomerID == cusID));
                    }
                    else
                    {
                        Global.SetMessage(lblMessage, "Xóa không thành công", false);
                    }
                
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateCustomer(dtgCustomerList.CurrentRow.Index);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            int n = dtgCustomerList.SelectedRows.Count;
            if ( n > 0)
            {
                for (int i = 0; i < n; i++)
                {
                    rowIndex = dtgCustomerList.SelectedRows[0].Index;
                    int cusID = int.Parse(dtgCustomerList[colCustomerID.Index, rowIndex].Value.ToString());
                    DeleteCustomer(cusID);
                }
            }
        }

        public void CustomerChangeAfterInsert(CustomerDTO cusDTO)
        {
            CustomerDTO customerDTO = customerBUS.GetCustomerByID(cusDTO.CustomerID);
          
            listCustomerBinding.Add(customerDTO);

            rowIndex = dtgCustomerList.Rows.GetLastRow(DataGridViewElementStates.None);
            dtgCustomerList[colEdit.Index, rowIndex].Value = Properties.Resources.edit_16;
            dtgCustomerList[colDel.Index, rowIndex].Value = Properties.Resources.deletered;
            //Global.GenerateNumber(dtgCategoryList, colNumber.Index);
            if (rowIndex == 0)
            {
                dtgCustomerList[colNumber.Index, rowIndex].Value = rowIndex + 1;
            }
            else
            {
                dtgCustomerList[colNumber.Index, rowIndex].Value = (int.Parse(dtgCustomerList[colNumber.Index, rowIndex - 1].Value.ToString()) + 1).ToString();
            }
        }

        public void CustomerChanged(CustomerDTO cusDTO)
        {
            CustomerDTO customerDTO = customerBUS.GetCustomerByID(cusDTO.CustomerID);

            dtgCustomerList[colCustomerName.Index, rowIndex].Value = customerDTO.CustomerName;
            dtgCustomerList[colCustomerAddress.Index, rowIndex].Value = customerDTO.CustomerAddress;
            dtgCustomerList[colCustomerBirthday.Index, rowIndex].Value = customerDTO.CustomerBirthday;
            dtgCustomerList[colCustomerCMND.Index,rowIndex].Value = customerDTO.CustomerCMND;
            dtgCustomerList[colCustomerEmail.Index, rowIndex].Value = customerDTO.CustomerEmail;
            dtgCustomerList[colCustomerPhone.Index, rowIndex].Value = customerDTO.CustomerPhone;
            dtgCustomerList[colCustomerSex.Index,rowIndex].Value = customerDTO.CustomerSexString;
            dtgCustomerList[colCustomerNote.Index, rowIndex].Value = customerDTO.Note;
        }
    }
}
